import 'dart:convert';
import 'package:flutter/material.dart';



// 提供五套可选主题色
const _themes = <MaterialColor>[
  Colors.blue,
  Colors.cyan,
  Colors.teal,
  Colors.green,
  Colors.red,
];

class Global {
  static late SharedPreferences _pref;
  static Profile profile = Profile();
  // 网络缓存对象
  static NetCache netCache = NetCache();

  // 可选的主题列表
  static List<MaterialColor> get themes => _themes;

  // 是否为release版
  static bool get isRelease => const bool.fromEnvironment("dart.vm.product");

  //初始化全局信息，会在APP启动时执行
  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    _pref = await SharedPreferences.getInstance();
    var profile = _pref.getString("profile",'1234');
    if (profile != null) {
      try {
        profile = Profile.fromJson(jsonDecode(profile));
      } catch (e) {
        print(e);
      }
    } else {
      // 默认主题索引为0，代表蓝色
      profile = Profile()..theme = 0;
    }

    // 如果没有缓存策略，设置默认缓存策略
    profile.cache = profile.cache ?? CacheConfig()
      ..enable = true
      ..maxAge = 3600
      ..maxCount = 100;

    //初始化网络请求相关配置
    Git.init();
  }

  // 持久化Profile信息
  static saveProfile() =>
      _pref.getString("profile", jsonEncode(profile.toJson()));
}

class Git {
  static void init() {}
}

class CacheConfig {
}

class NetCache {
}

class Profile {
  set theme(int theme) {}

  static fromJson(jsonDecode) {}
  
  Object? toJson() {
    return null;
  }
}

class SharedPreferences {
  static getInstance() {}
  
  getString(String s, String jsonEncode) {}
}
